package the.broccoli.setting.service.impl;

import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.wisteria.framework.web.ExceptionMessage;
import org.wisteria.framework.web.mvc.service.AbstractSuperService;
import the.broccoli.setting.dto.LanguageCondition;
import the.broccoli.setting.entity.Language;
import the.broccoli.setting.mapper.LanguageMapper;
import the.broccoli.setting.service.ILanguageService;

import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * <p>
 * 多语言 服务实现类
 * </p>
 *
 * @author lei.liu
 * @since 2020-04-18
 */
@Service
public class LanguageServiceImpl extends AbstractSuperService<LanguageMapper, Language> implements ILanguageService {

    @Transactional
    @Override
    public void save(String constant, String code, String nameZhCn, String operatorId, String operatorName) {
        if (!StringUtils.hasText(constant) || !StringUtils.hasText(code)) {
            return;
        }

        LanguageCondition condition = new LanguageCondition();
        condition.setConstant(constant);
        condition.setLanguageCode(code);
        List<Language> languageList = listByCondition(condition);
        Language entity = null;
        if (CollectionUtils.isEmpty(languageList)) {
            entity = new Language();
            entity.setConstant(constant);
            entity.setLanguageCode(code);
            entity.setLanguageZhCn(nameZhCn);
            entity.setLanguageEnUs(code);
            entity.setCreatedId(operatorId);
            entity.setCreatedName(operatorName);
            entity.setCreatedTime(new Date());
            save(entity);
        } else {
            entity = languageList.get(0);
            entity.setLanguageZhCn(nameZhCn);
            entity.setModifiedId(operatorId);
            entity.setModifiedName(operatorName);
            entity.setModifiedTime(new Date());
            updateById(entity);
        }
    }

    @Transactional
    @Override
    public void removeByCode(String constant, String code, String operatorId, String operatorName) {
        baseMapper.deleteByCode(constant, code, operatorId, operatorName, new Date());
    }

    @Override
    public String getMessage(String msgCode) {
        return getMessage(msgCode, null);
    }

    @Override
    public String getMessage(String msgCode, String[] args) {
        Language language = getByCode(msgCode);
        String message = null;
        if (language != null) {
            Locale locale = LocaleContextHolder.getLocale();
            String lang = locale.getLanguage();
            if (Locale.US.getLanguage().equals(lang)) {
                message = language.getLanguageEnUs();
            } else {
                message = language.getLanguageZhCn();
            }
        }

        return StringUtils.hasText(message) ? message : msgCode;
    }

    private Language getByCode(String code) {
        LanguageCondition condition = new LanguageCondition();
        condition.setLanguageCode(code);
        List<Language> list = listByCondition(condition);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }

}
